#!/bin/bash
# Usage:
#   curl https://raw.githubusercontent.com/mixool/script/master/kcptun.sh | bash

# Color
export green='\033[0;32m'
export plain='\033[0m'

# Export 
export URL="https://raw.githubusercontent.com/mixool/script/source/server_linux_amd64"
export NAME="server_linux_amd64"
export DO="-l :11001 -t 127.0.0.1:11000"

if [ "$(id -u)" != "0" ]; then
    echo "ERROR: Please run as root"
    exit 1
fi

echo -e "${green}Clean up $NAME${plain}"
systemctl disable $NAME.service
killall -9 $NAME
rm -rf /root/$NAME /etc/systemd/system/$NAME.service

echo "Download $NAME from $URL"
curl -L "${URL}" >/root/$NAME
chmod +x /root/$NAME

echo "Generate /etc/systemd/system/$NAME.service"
cat <<EOF > /etc/systemd/system/$NAME.service
[Unit]
Description=$NAME

[Service]
ExecStart=/root/$NAME $DO
Restart=always
User=root

[Install]
WantedBy=multi-user.target
EOF

echo "Enable $NAME Service"
systemctl enable $NAME.service

echo "Start $NAME Service"
systemctl start $NAME.service

if systemctl status $NAME >/dev/null; then
	echo "$NAME started:"
	echo -e "${green}vi /etc/systemd/system/$NAME.service${plain} as needed."
	echo -e "${green}systemctl daemon-reload && systemctl restart $NAME.service${plain} for restart."
else
	echo "$NAME start failed."
fi
